package com.zhbcm.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/4/23 21:42
 */
public class TimeServerHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
    {
        String request = (String) msg;
        String response = null;
        if ("QUERY TIME ORDER".equals(request))
        {
            response = new Date(System.currentTimeMillis()).toString();
        } else
        {
            response = "BAD REQUEST";
        }
        response = response + System.getProperty("line.separator");
        ByteBuf resp = Unpooled.copiedBuffer(response.getBytes());
        ctx.writeAndFlush(resp);
    }
}
